<template>
  <div class="student-list">
    <div class="search">
      <el-form :inline="true" :model="searchCondition" class="demo-form-inline">
        <el-form-item label="姓名">
          <el-input
            v-model="searchCondition.name"
            placeholder="姓名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="compPage" border style="width: 100%">
      <el-table-column prop="name" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="age" label="年龄" align="center">
      </el-table-column>
      <el-table-column prop="sex_text" label="性别" align="center">
      </el-table-column>
      <el-table-column prop="number" label="学号" align="center">
      </el-table-column>
      <el-table-column prop="class" label="班级" align="center">
      </el-table-column>
      <el-table-column prop="state_text" label="状态" align="center">
      </el-table-column>
      <el-table-column prop="address" label="地址" align="center">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <!-- slot-scope="scope" 中的scope能够拿到这一行和这一列的数据, 注意需要写在template上 -->
        <template slot-scope="scope">
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="deleteOne(scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pageDevice">
      <PagingDevice
        :currentPage="condition.page"
        :pagesize="condition.pagesize"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20, 25, 30]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination> -->
  </div>
</template>

<script>
// import { getStudentList, deleteOneFromStudent } from "@/api/api";
import PagingDevice from "@/components/common/PagingDevice.vue";
import { getTableData, deleteTableData } from "@/utils/table";
import pageDeviceMixin from "@/mixins/pageDeviceMixin";

export default {
  mixins: [pageDeviceMixin],
  components: {
    PagingDevice,
  },
  data() {
    return {
      tableData: [],
      searchCondition: {
        // 表单查询条件
        name: "", // 通过名字查询
      },
    };
  },
  methods: {
    // handleSizeChange({ _pagesize, _currentPage }) {
    //   // 子传父数据 _pagesize, _currentPage
    //   this.pagesize = _pagesize;
    //   this.currentPage = _currentPage;
    // },
    // handleCurrentChange({ _currentPage }) {
    //   // 子传父数据 _currentPage
    //   this.currentPage = _currentPage;
    // },
    async getData(params) {
      // 根据params条件进行查询
      const res = await getTableData(this, "/students", params);
      this.tableData = res.data;
      this.total = res.total;
    },
    onSubmit() {
      // 提交查询表单的时候，只需要将条件传过去给getData让它带着条件查询即可
      this.getData(this.searchCondition); // 将条件传过去
    },
    reset() {
      // 重置表格数据为原始数据, 也就是去掉查询条件然后再重新无条件渲染全部
      this.searchCondition.name = "";
      this.getData();
    },
    async deleteOne({ id }) {
      // 因为传过来的是这一行数据的对象row, 通过结构出这一行数据的id
      try {
        // 当用户点击取消的时候会抛出异常，当用户点击确认时返回'confirm', 所以只需要在捕捉到异常的时候return就行
        await this.$confirm("是否确认删除数据?", "提示", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "warning",
        });
      } catch (error) {
        return;
      }

      const res = await deleteTableData(this, "/students", id);
      if (res.status === 200) {
        // 删除成功后重新调用最新数据渲染, 注意如果有查询条件的话需要携带查询条件
        let currentTotalPage = Math.ceil(
          (this.total - 1) / this.condition.size
        ); // 向上取整就能得到总页数, this.total - 1 这里删除成功后要先减一，因为这里的total还没有更新需要手动减一
        if (
          this.condition.page > currentTotalPage &&
          this.condition.page != 1
        ) {
          // 如果当前页数大于总页数，说明最后一页的数据全部删除完毕了, 让当前页跳到上一页, 并且当前也不能为1，不然的话它会停留在已经没有数据的那一页
          this.condition.page--;
        }
        this.getData(this.searchCondition);
      }
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped></style>
